package editor.tester;

import org.jwalk.ReportEvent;
import org.jwalk.ReportListener;
import org.jwalk.out.AlgebraReport;
import org.jwalk.out.CycleReport;
import org.jwalk.out.Edition;
import org.jwalk.out.ProtocolReport;
import org.jwalk.out.Report;
import org.jwalk.out.StateReport;
import org.jwalk.out.SummaryReport;

import editor.manager.WorkSpaceManager;


public class ReportPublisher implements ReportListener 
{

	public ReportPublisher() 
	{
	}
  
	public void publish(ReportEvent event) {
		
		Report report = event.getReport();
		Edition edition = report.getEdition();
		switch (edition) 
		{
			case PROTOCOL_REPORT :
				handle((ProtocolReport) report);
				break;
			case ALGEBRA_REPORT :
				handle((AlgebraReport) report);
				break;
			case STATE_REPORT :
				handle((StateReport) report);
				break;
			case CYCLE_REPORT :
				handle((CycleReport) report);
				break;
			case SUMMARY_REPORT :
				handle((SummaryReport) report);
				break;
			default :
				break;
		}
		
	}
	
	/**
	 * Publishes a ProtocolReport dispatched by a JWalker.
	 */
	protected void handle(ProtocolReport report) {
		String title = report.toString(report.getTestClass()) + " Protocol Analysis";
		
		WorkSpaceManager.testingManager.addOutput(title, report.getContent());
	}
	
	/**
	 * Publishes an AlgebraReport dispatched by a JWalker.
	 */
	protected void handle(AlgebraReport report) {
		String title = report.toString(report.getTestClass()) + " Algebraic Analysis";
		WorkSpaceManager.testingManager.addOutput(title, report.getContent());
	}
	
	/**
	 * Publishes a StateReport dispatched by a JWalker.
	 */
	protected void handle(StateReport report) {
		String title = report.toString(report.getTestClass()) +  " State Space Analysis";
		WorkSpaceManager.testingManager.addOutput(title, report.getContent());
	}
	
	/**
	 * Publishes a CycleReport dispatched by a JWalker.
	 */
	protected void handle(CycleReport report) {
		String state = report.getStartingState();
		String title = "Test Cycle #" + report.getTestCycle();
		if (state != null)
		{
			title = state + ": " + title;
		}
		WorkSpaceManager.testingManager.addOutput(title, report.getContent());
	}
	
	/**
	 * Publishes a SummaryReport dispatched by a JWalker.
	 */
	protected void handle(SummaryReport report) {
		String title = report.toString(report.getTestClass()) + " Test Summary";
		WorkSpaceManager.testingManager.addOutput(title, report.getContent());
	}
	

}
